// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class OrderDetailsMapper
    {

        /// <summary />
        static OrderDetailsMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<OrderDetails> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.OrderDetailsSelectAll);

            return Load(commands, dataMappers);
        }

        private OrderDetails.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            OrderDetails.DataTransfer data=new OrderDetails.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static OrderDetails.DataTransfer FillData(OrderDetails.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("OrderID");
            if (!record.IsDBNull(ord))
                data.OrderID=record.GetInt32(ord);

            ord=record.GetOrdinal("ProductID");
            if (!record.IsDBNull(ord))
                data.ProductID=record.GetInt32(ord);

            ord=record.GetOrdinal("UnitPrice");
            if (!record.IsDBNull(ord))
                data.UnitPrice=record.GetDecimal(ord);

            ord=record.GetOrdinal("Quantity");
            if (!record.IsDBNull(ord))
                data.Quantity=record.GetInt16(ord);

            ord=record.GetOrdinal("Discount");
            if (!record.IsDBNull(ord))
                data.Discount=record.GetFloat(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, OrderDetails.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderDetailsSelect,
                    parameterBuilder.GetMarker("OrderID", 1),
                    parameterBuilder.GetMarker("ProductID", 2)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderDetailsDelete,
                    parameterBuilder.GetMarker("OrderID", 1),
                    parameterBuilder.GetMarker("ProductID", 2)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ProductID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderDetailsInsert,
                    parameterBuilder.GetMarker("OrderID", 1),
                    parameterBuilder.GetMarker("ProductID", 2),
                    parameterBuilder.GetMarker("UnitPrice", 3),
                    parameterBuilder.GetMarker("Quantity", 4),
                    parameterBuilder.GetMarker("Discount", 5)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 2),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ProductID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitPrice", 3),
                DbType.Decimal,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitPrice
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Quantity", 4),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Quantity
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Discount", 5),
                DbType.Single,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Discount
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.OrderDetailsUpdate,
                    parameterBuilder.GetMarker("UnitPrice", 1),
                    parameterBuilder.GetMarker("Quantity", 2),
                    parameterBuilder.GetMarker("Discount", 3),
                    parameterBuilder.GetMarker("OrderID", 4),
                    parameterBuilder.GetMarker("ProductID", 5)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("UnitPrice", 1),
                DbType.Decimal,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.UnitPrice
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Quantity", 2),
                DbType.Int16,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Quantity
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Discount", 3),
                DbType.Single,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.Discount
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("OrderID", 4),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.OrderID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ProductID", 5),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.ProductID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
